<?php 
	require_once("init.php");  
?>

<?php
	// randomise random moderation query seed
	if (!isset($_SESSION["randomModerationSeed"]) || isset($randomiseModerationSeed)) {
		$_SESSION["randomModerationSeed"] = (rand()%9999);
	}
	
	$page = 0;
	if (isset($_GET["page"])) {
		$page = $_GET["page"];
	}
	$startRow = $page * $topicsPerPage;
	$endRow = ($page+1) * $topicsPerPage;
	
	// Topics Count
	$strTopicsCount = "
    SELECT 
			count(topicId) AS totalCount
    FROM topics
    WHERE statusId = ".$statusActive."
			AND now() < dateClosed
			AND points > 0
			AND dateReported IS NOT NULL
	";
	if (isset($_SESSION["userId"])) {
		$strTopicsCount = $strTopicsCount."
			AND topicId NOT IN (
				SELECT topicId
				FROM moderations
				WHERE userId = ".$_SESSION["userId"]."
			)
		";
	} else if (strlen(session_id()) > 0) {
		$strTopicsCount = $strTopicsCount."
			AND topicId NOT IN (
				SELECT topicId
				FROM moderations
				WHERE sessionId = '".session_id()."'
			)
		";
	}
	$strTopicsCount = $strTopicsCount."
    ORDER BY rand(".$_SESSION["randomModerationSeed"].")
		LIMIT 0, ".$maxTopicsNum."
	";
  $qTopicsCount = mysql_query($strTopicsCount);

  // Topics
	$strTopics = "
    SELECT 
      t.*,
      c.name AS categoryName,
      (dateReported >= subTime(now(), '24:00:00')) AS isNew
    FROM topics t
      INNER JOIN categories c ON t.categoryId = c.categoryId
    WHERE statusId = ".$statusActive."
			AND now() < t.dateClosed
			AND points > 0
			AND dateReported IS NOT NULL
	";
	if (isset($_SESSION["userId"])) {
		$strTopics = $strTopics."
			AND topicId NOT IN (
				SELECT topicId
				FROM moderations
				WHERE userId = ".$_SESSION["userId"]."
			)
		";
	} else if (strlen(session_id()) > 0) {
		$strTopics = $strTopics."
			AND topicId NOT IN (
				SELECT topicId
				FROM moderations
				WHERE sessionId = '".session_id()."'
			)
		";
	}
	$strTopics = $strTopics."
    ORDER BY rand(".$_SESSION["randomModerationSeed"].")
    LIMIT ".$startRow.", ".$endRow.";
  ";
  $qTopics = mysql_query($strTopics);
	
	$showMore = true;
	$totalCount = mysql_fetch_array($qTopicsCount);
	$totalCount = $totalCount["totalCount"];
	if ($endRow >= $totalCount) $showMore = false;
	
	if (mysql_num_rows($qTopics) == 0) {
?>
<li>
	No topics found
</li>
<?php
	} else {
		while ($row = mysql_fetch_array($qTopics)) {
?>
<li <?php if ($row["isNew"] == 1) print "class=\"new\""; ?>>
	<a href="viewreportedtopic.php?id=<?php print $row["topicId"]; ?>">
		<?php print $row["question"]; ?><br />
		<small><strong><?php print $row["answer1"]." or ".$row["answer2"]; ?>?</strong></small>
	</a>
</li>
<?php
		}
	}
?>

<?php
	if ($showMore) {
?>
<li class="more">
	<a href="listreportedtopic.php?page=<?php print ($page+1) ?>" target="_replace"><?php print $topicsPerPage; ?> more topics...</a>
</li>
<?php
	}
?>